<template>
  <el-dialog v-model="dialogVisible" title="编辑边线" width="900" destroy-on-close lock-scroll center
             :close-on-click-modal="false">
    <el-form ref="elFormRef" v-if="currentEdge" :model="currentEdge.properties" :rules="rules" label-width="120px">
      <el-form-item v-if="sourceNode.type === 'BooleanNode'" label="边值" prop="value">
        <el-select v-model="currentEdge.properties.value" placeholder="请选择边值" size="small">
          <el-option :key="'true'" :label="'true'" :value="'true'"/>
          <el-option :key="'false'" :label="'false'" :value="'false'"/>
        </el-select>
      </el-form-item>

      <el-form-item v-if="sourceNode.type === 'SwitchNode'" label="标签" prop="tag">
        <el-input v-model="currentEdge.properties.tag" placeholder="请输入标签" @change="handleTagChange"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancelClick">取消</el-button>
        <el-button type="primary" @click="handleConfirmClick">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="js">
import {ref} from 'vue'

const dialogVisible = ref(false);
const elFormRef = ref();
const currentEdge = ref({});
const sourceNode = ref({});
const targetNode = ref({});
const rules = ref({
  value: [
    {required: true, message: "值不能为空", trigger: "blur"}
  ],
  tag: [
    {required: true, message: "标签不能为空", trigger: "blur"}
  ],
});

const emit = defineEmits(['updateEdge']);

const handleTagChange = (tag) => {
  console.log(tag);
  currentEdge.value.properties.value = tag;
}

const handleCancelClick = () => {
  dialogVisible.value = false;
}

const handleConfirmClick = () => {
  elFormRef.value.validate(async (valid) => {
    if (valid) {
      emit('updateEdge', currentEdge.value)
      dialogVisible.value = false;
    }
  });
}

const resetDialog = () => {
  currentEdge.value = {};
  sourceNode.value = {};
  targetNode.value = {};
}

const openDialog = (sNode, tNode, edgeData) => {
  resetDialog();
  currentEdge.value = edgeData;
  sourceNode.value = sNode;
  targetNode.value = tNode;
  dialogVisible.value = true;
  console.log(sourceNode.value)
  console.log(targetNode.value)
}

defineExpose({openDialog});
</script>